package com.example.opengl.camera.filter

import android.content.Context
import android.opengl.GLES20.*
import android.util.Log

/**
 * @author: Mr.You
 * @create: 2023-10-16 14:51
 * @description:
 **/
class FilterGroup() {
    companion object {
        const val TAG = "FilterGroup"
    }

    private var baseFilter: BaseFilter? = null

    private val fFrame = IntArray(1)

    private var width = 0
    private var height = 0
    private var matrix: FloatArray? = null

    init {
        baseFilter = NoneFilter()
    }

    fun switchFilter(filterType: FilterType) {
        //z  Log.i(TAG, "switchFilter: ${filterType.name} ，mWidth：$mWidth")
        baseFilter = getFilterByFilterType(filterType)
        baseFilter?.onReady(width, height)
    }


    fun onReady(width: Int, height: Int) {
        this.width = width
        this.height = height
          baseFilter?.onReady(width, height)

    }

    fun setMatrix(matrix: FloatArray) {
        this.matrix = matrix
    }

    fun onDrawFrame(textureId: Int): Int {
        glEnable(GL_SCISSOR_TEST)
        val id = baseFilter?.onDrawFrame(textureId)
        glDisable(GL_SCISSOR_TEST)
        return id!!
    }

    private fun getFilterByFilterType(filterType: FilterType): BaseFilter {
        return when (filterType) {
            FilterType.WARM -> {
                WarmFilter()
            }
            FilterType.COOL ->{
                CoolFilter()
            }
            FilterType.NEGATIVE ->{
                NegativeFilter()
            }
            FilterType.OLD ->{
                OldFilter()
            }
            FilterType.DREAM ->{
                DreamFilter()
            }
            FilterType.RELIEF ->{
                ReliefFilter()
            }
            FilterType.BLACK_AND_WHITE->{
                BlackAndWhiteFilter()

            }


            else -> {
                NoneFilter()
            }
        }
    }
}